package net.disy.cadenza.repository.wrappers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SelectorCondition {
	private String conditionPrintName;
	private String conditionName;
	private String objectType;
	private List<DefaultConditionValue> defaultValues = new ArrayList<DefaultConditionValue>();
	private int MAX_COUNT_DEFAULT_VALUES = 50;
	
	public SelectorCondition(String objectType, String conditionName){
		this.conditionName = conditionName;
		this.objectType = objectType;
	}
	
	public void addDefaultValue(DefaultConditionValue defaultValue){
		if(defaultValues.size() == MAX_COUNT_DEFAULT_VALUES)
			return;
		for(DefaultConditionValue containedDefaultValue : defaultValues)
			if(containedDefaultValue.equalTo(defaultValue))
				return;
		defaultValues.add(defaultValue);
	}
	
	public String getObjectType() {
		return objectType;
	}

	public void setObjectType(String objectType) {
		this.objectType = objectType;
	}

	public String getConditionName() {
		return conditionName;
	}
	
	

	public String getConditionPrintName() {
		return conditionPrintName;
	}

	public void setConditionPrintName(String conditionPrintName) {
		this.conditionPrintName = conditionPrintName;
	}

	public List<DefaultConditionValue> getDefaultValues() {
		Collections.sort(defaultValues, new DefaultConditionValueComparator());
		return defaultValues;
	}

	public boolean equalTo(SelectorCondition otherSelectorCondition){
		if(conditionName.equalsIgnoreCase(otherSelectorCondition.getConditionName())){
			if(defaultValues.size() == otherSelectorCondition.getDefaultValues().size()){
				for(int i = 0; i < defaultValues.size(); i++){
					DefaultConditionValue defaultConditionValue = this.defaultValues.get(i);
					DefaultConditionValue otherDefaultConditionValue = otherSelectorCondition.getDefaultValues().get(i);
					if(!this.containsCondition(otherDefaultConditionValue) || !otherSelectorCondition.containsCondition(defaultConditionValue))
						return false;
				}
			}
		}
		return true;
	}
	
	public boolean containsCondition(DefaultConditionValue conditionValue){
		for(DefaultConditionValue defaultConditionValue : defaultValues)
			if(defaultConditionValue.equalTo(defaultConditionValue))
				return true;
		return false;
	}
	
	public boolean hasEnabledDefaultValue(){
		for(DefaultConditionValue defaultConditionValue : getDefaultValues())
			if(defaultConditionValue.isEnabled())
				return true;
		return false;
	}
	
	public String toString(){
		return conditionName + " " + defaultValues;
	}
}

class DefaultConditionValueComparator implements Comparator<DefaultConditionValue>{
	public int compare(DefaultConditionValue o1, DefaultConditionValue o2) {
		if(o1 == null || o2 == null || o1.getMainAttributeValue() == null || o1.getMainAttributeValue() == null)
			return 0;
		return o1.getMainAttributeValue().compareTo(o2.getMainAttributeValue());
	}
	
}
